package com.culturaltourism.core.dto;

import com.culturaltourism.core.enums.ResultEnum;
import com.culturaltourism.core.wraps.URL;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName: Result
 * @Description: 响应数据(结果)最外层对象工具
 * @Author MaShuai
 * @Date 2019/5/1 18:46
 * @Version V1.0
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class Result<T> extends HashMap<String, Object> implements Serializable {

    private static final long serialVersionUID = -7525143028163917712L;

    /**
     * 状态码
     */
    private Integer code;
    /**
     * 提示信息 
     */
    private String msg;
    /**
     * 返回的数据总数 
     */
    private Long count;
    /**
     * 响应数据
     */
    private T data;

    public static Result SAVE_SUCCESS = success("保存成功");

    /**
     * 操作成功
     * @param list 对象
     */
    public static Result success(List<?> list){
        Result result = new Result();
        PageInfo<?> pageInfo = new PageInfo<>(list);
        result.setMsg(ResultEnum.SUCCESS.getMessage());
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setCount(pageInfo.getTotal());
        result.setData(list);
        return result;
    }

    /**
     * 操作成功
     * @param msg 提示信息
     * @param list 对象
     */
    public static Result success(String msg, List<?> list){
        Result result = new Result();
        result.setMsg(msg);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setCount(new PageInfo(list).getTotal());
        result.setData(list);
        return result;
    }

    /**
     * 操作成功
     * @param object 对象
     */
    public static Result success( String msg, Long count, Object object){
        Result result = new Result();
        result.setMsg(msg);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setCount(count);
        result.setData(object);
        return result;
    }

    /**
     * 操作成功
     * @param object 对象
     */
    public static Result success( Integer code, String msg, Object object){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setData(object);
        return result;
    }

    /**
     * 操作成功
     * @param msg 提示信息
     * @param object 对象
     */
    public static Result success(String msg, Object object){
        Result result = new Result();
        result.setMsg(msg);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setData(object);
        return result;
    }

    /**
     * 操作成功，返回url地址
     * @param msg 提示信息
     * @param url URL包装对象
     */
    public static Result success(String msg, URL url){
        return success(msg,url.getUrl());
    }

    /**
     * 操作成功，使用默认的提示信息
     * @param object 对象
     */
    public static Result success(Object object){
        String message = ResultEnum.SUCCESS.getMessage();
        return success(message,object);
    }

    /**
     * 操作成功，返回提示信息，不返回数据
     */
    public static Result success(String msg){
        Result result = new Result();
        result.setMsg(msg);
        result.setCode(ResultEnum.SUCCESS.getCode());
        return result;
    }

    /**
     * 操作成功，不返回数据
     */
    public static Result success(){
        Result result = new Result();
        result.setMsg(ResultEnum.SUCCESS.getMessage());
        result.setCode(ResultEnum.SUCCESS.getCode());
        return result;
    }

    /**
     * 操作有误
     * @param code 错误码
     * @param msg 提示信息
     */
    public static Result error(Integer code, String msg){
        Result result = new Result();
        result.setMsg(msg);
        result.setCode(code);
        return result;
    }

    /**
     * 操作有误，使用默认400错误码
     * @param msg 提示信息
     */
    public static Result error(String msg){
        Integer code = ResultEnum.ERROR.getCode();
        return error(code,msg);
    }
}
